﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ResponseMonitor
{
    public partial class NewFromForm : Form
    {
        public string ProjectName = string.Empty;
        public string ExistProjectPath = string.Empty;
        public string ProjectPath = string.Empty;
        public string TestData = string.Empty;

        public NewFromForm()
        {
            InitializeComponent();
        }

        private void buttonSelectProject_Click(object sender, EventArgs e)
        {
            textBoxTestData.Text = string.Empty;

            DialogResult result = folderBrowserDialogLocation.ShowDialog();
            if (result == DialogResult.OK)
            {
                string folderName = folderBrowserDialogLocation.SelectedPath;
                textBoxExistProject.Text = folderName;
            }

            //  把选择的项目的测试数据显示出来供用户修改. 如果没有测试文件则不显示(意味着选择的项目有问题, 在父窗口处理)
            string fileName = textBoxExistProject.Text;
            if (!fileName.EndsWith(@"\")) fileName += @"\";
            fileName += "TestCase.msc";

            if (!File.Exists(fileName))
            {
                TestData = "There is no TestCase file.";
            }
            else
            {
                try
                {
                    using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
                    {
                        TestData = sr.ReadToEnd();
                    }
                }
                catch (Exception ex)
                {
                    // todo: 记录错误日志
                    TestData = "There is no TestCase file.";
                }
            }

            textBoxTestData.Text = TestData;
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialogLocation.ShowDialog();
            if (result == DialogResult.OK)
            {
                string folderName = folderBrowserDialogLocation.SelectedPath;
                textBoxLocation.Text = folderName;
            }
        }

        private void NewFromForm_Load(object sender, EventArgs e)
        {
            TestData = string.Empty;

            textBoxProjectName.Text = ProjectName;
            textBoxExistProject.Text = ExistProjectPath;
            textBoxLocation.Text = ProjectPath;
            textBoxTestData.Text = TestData;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;

            ProjectName = textBoxProjectName.Text.Trim();
            ExistProjectPath = textBoxExistProject.Text.Trim();
            ProjectPath = textBoxLocation.Text.Trim();
            TestData = textBoxTestData.Text.Trim();

            this.Close();
        }

        public void ClearData()
        {
            ProjectName = string.Empty;
            ExistProjectPath = string.Empty;
            ProjectPath = string.Empty;
            TestData = string.Empty;
        }
    }
}
